﻿using STHPMS1.Common.C_Entity;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.ICore;
using STHPMS1.ICore.I_Core;
using STHPMS1.ICore.I_UI;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace STHPMS1.Web.W_Controller.Decision.Preventive
{
    [RoutePrefix("api/prevendetail")]
    public class DP_PrevenDetailController : STHPMS1.Web.W_Core.MyController
    {
        [Route("")]
        [Route("page/{page?}", Name = "GetPrevenDetailByPage")]
        [Route("rows/{rows?}")]
        [Route("page/{page?}/rows/{rows?}")]
        public IHttpActionResult Get(string anniname=null,string analy_year = null, int page = 1, int rows = 10)
        {
            int p = page;
            int s = rows;

            PagingInfo info = new PagingInfo();
            info.PageIndex = p > 0 ? p - 1 : 0;
            info.PageSize = s;
            info.TotalRecords = 0;

            Expression<Func<Dp_Preven_Detail, bool>> exp = null;
            if (anniname != null && anniname.ToString().Length > 0 && analy_year != null && analy_year.ToString().Length > 0)
                exp = pre =>pre.Anni_Name==anniname.ToString() && pre.Analy_Year == analy_year.ToString();

            var data = this.Scene.Bll.Dp_Preven_Detail.Get<Dp_Preven_Detail>(exp, info).ToArray();
            if (data == null) return NotFound();

            var result = new { rows = data, total = info.TotalRecords };

            return Ok(result);
        }


        [Route("")]
        public HttpResponseMessage Post(Dp_Preven_Detail entity)
        {
            //entity.Importdate = DateTime.Now;
            this.Scene.Bll.Dp_Preven_Detail.Save(entity);

            var response = Request.CreateResponse(HttpStatusCode.Created);
            // Generate a link to the new book and set the Location header in the response.
            string uri = Url.Link("GetPrevenDetailByPage", new { page = 1 });
            response.Headers.Location = new Uri(uri);

            return response;
        }

    
        /// <summary>
        /// 计算等效年度费用
        /// </summary>
        /// <param name="rdwayid"></param>
        [Route("seteac")]
        public void GetSetEAC(string anniname,string analy_year)
        {
            this.Scene.Bll.Dp_Preven_Detail.SetEAC(anniname, analy_year);
        }
        /// <summary>
        /// 自动选取复选措施
        /// </summary>
        /// <param name="rdwayid"></param>
        [Route("setseconmeasure")]
        public void GetSetSECON_MEASURE(string anniname, string analy_year)
        {
            this.Scene.Bll.Dp_Preven_Detail.SetSECON_MEASURE(anniname, analy_year, 1);
        }

        ///// <summary>
        ///// 计算综合评判系数
        ///// </summary>
        ///// <param name="rdwayid"></param>
        //[Route("setcoef")]
        //public void GetJUDGE_COEF(string analy_year)
        //{
        //    this.Scene.Bll.Dp_Preven_Detail.SetJUDGE_COEF(analy_year);
        //}
    }
}